#include "widget.h"
#include "ui_widget.h"


//runtime_error
//logic_error
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    db = QSqlDatabase::addDatabase("QSQLITE");
    if(!db.isValid())
    {
        QMessageBox::critical(this,"严重错误","数据库驱动加载失败");
        throw std::runtime_error("数据库驱动加载失败");
    }

    db.setDatabaseName("..\\..\\testDB.db");
    if(!db.open())
    {
        QMessageBox::critical(this,"严重错误","数据库打开失败");
        throw std::runtime_error("数据库打开错误: " + db.lastError().text().toStdString());
    }

    QSqlQueryModel* model = new QSqlQueryModel;
    model->setQuery("SELECT * FROM employee1;");
    if(model->lastError().isValid())
    {
        QMessageBox::critical(this,"错误","执行查询数据库语句错误");
        throw std::runtime_error("数据库执行查询语句失败:"+model->lastError().text().toStdString());
    }

    QTableView* tv_list = new QTableView(this);
    tv_list->setModel(model);

    QVBoxLayout* vBoxLayout = new QVBoxLayout;
    vBoxLayout->addWidget(tv_list);
    this->setLayout(vBoxLayout);

}

Widget::~Widget()
{
    delete ui;
}
